package com.eqdd.yiqidian.ui.tongxunlu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.view.View;

import com.bigkoo.alertview.OnItemClickListener;
import com.eqdd.yiqidian.QunErweimaActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.ErweimaBean;
import com.eqdd.yiqidian.share.ImgShareBuilder;
import com.eqdd.yiqidian.share.JShareDialog;
import com.eqdd.yiqidian.ui.ScanZXingActivity;
import com.eqdd.yiqidian.utils.FileUtil;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.MyCodeUtil;
import com.eqdd.yiqidian.utils.PermissionTipUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;

import static android.Manifest.permission.CAMERA;

//import com.uuzuche.lib_zxing.activity.CodeUtils;

public class QunErweimaActivity extends BaseActivity {
    private QunErweimaActivityCustom dataBinding;
    private String groupid;
    private String groupname;
    private Bitmap mBitmap;
    private String result;
    private ActionSheetDialog bottomChoose;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_qun_erweima);
        initTopTitleBar(View.VISIBLE, "群二维码");
        initTopRightText("更多", v ->
                bottomChoose.show()
        );
    }

    /*
    asdf   /aa/bb/cc/xxxxx/   afsdfa
     */
    @Override
    public void initData() {
        groupid = getIntent().getStringExtra("groupid");
        groupname = getIntent().getStringExtra("groupname");
        initDialog();
    }

    private void initDialog() {
        bottomChoose = ChooseAlertView.getBottomChoose(QunErweimaActivity.this, null, new String[]{"分享", "保存到手机", "扫描"}, new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {
                switch (position) {
                    case 1:
                        dataBinding.rlPrint.setDrawingCacheEnabled(true);
                        dataBinding.rlPrint.buildDrawingCache();
                        Bitmap shareBitmap = dataBinding.rlPrint.getDrawingCache();
                        File file = FileUtil.saveAsFile(shareBitmap);

                        ImgShareBuilder builder = new ImgShareBuilder(QunErweimaActivity.this);
                        builder.setImagePath(file.getAbsolutePath());
                        new JShareDialog(builder, QunErweimaActivity.this).show();
                        break;
                    case 2:
                        dataBinding.rlPrint.setDrawingCacheEnabled(true);
                        dataBinding.rlPrint.buildDrawingCache();
                        Bitmap saveBitmap = dataBinding.rlPrint.getDrawingCache();
                        File fileErweima = FileUtil.saveImageToGallery(QunErweimaActivity.this, saveBitmap);
                        ToastUtil.showShort("已成功保存到" + fileErweima.getAbsolutePath());
                        break;
                    case 3:
                        RxPermissions.getInstance(QunErweimaActivity.this)
                                .request(CAMERA)
                                .subscribe(granted -> {
                                    if (granted) {
                                        System.out.println("已授权");
                                        Intent intent = new Intent(QunErweimaActivity.this, ScanZXingActivity.class);
                                        startActivityForResult(intent, Config.SCAN);
                                    } else {
                                        PermissionTipUtil.tip(QunErweimaActivity.this, "存储");

                                    }
                                });

                }
            }
        });
    }

    @Override
    public void setView() {
        ErweimaBean erweimaBean = new ErweimaBean();
        erweimaBean.setName(groupname);
        erweimaBean.setType("1");
        erweimaBean.setUgid(groupid);
        MyCodeUtil.createImage(GsonUtils.createGsonString(erweimaBean), 400, new MyCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                QunErweimaActivity.this.mBitmap = bitmap;
                dataBinding.ivErweima.setImageBitmap(bitmap);
                dataBinding.tvName.setText(groupname);
            }

            @Override
            public void onAnalyzeFailed() {

                System.out.println("失败");
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
